package com.example.administrator.mpandroidchart;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.example.administrator.mpandroidchart.listener.IPresenter;
import com.example.administrator.mpandroidchart.listener.IView;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity implements IView {
    LineChart heart,breathe;
    TextView heart_show,breathe_show;

    List<Entry> hentries = new ArrayList<>();
    List<Entry> bentries = new ArrayList<>();
    LineDataSet lds;
    LineData ld;
    IPresenter presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        getPackageName();
        //实验
    }

    private void init() {
        find();
        presenter  = new Presenter();
        presenter.request();
        presenter.attachV(this);
        initChart();
    }

    private void find() {
        heart = (LineChart) findViewById(R.id.heart);
        breathe = (LineChart) findViewById(R.id.breathe);
        heart_show = (TextView) findViewById(R.id.heart_show);
        breathe_show = (TextView) findViewById(R.id.breathe_show);
    }

    /**
     *更新心率数据
     */
    @Override
    public void hreceiveData(Entry entry) {
        hentries.add(entry);
        int flag = 0;
        if(hentries.size()>200){
            for(Entry entry1:hentries){

                entry1.setX(flag);
                flag++;
            }
            flag = 0;
            hentries.remove(0);
        }
        heart.invalidate();
        heart_show.setText(entry.getY()+"");

    }
    /**
     *更新呼吸数据
     */
    @Override
    public void breceiveData(Entry entry) {
        bentries.add(entry);
        int flag = 0;
        if(bentries.size()>50){
            for(Entry entry1:bentries){

                entry1.setX(flag);
                flag++;
            }
            flag = 0;
            bentries.remove(0);
        }
        breathe.invalidate();
        breathe_show.setText(entry.getY()+"");
    }


    /**
     * 在这里初始化所有与图表有关的数据
     */
    private void initChart() {
        initHeart();
        initBreathe();
    }

    private void initHeart() {
        //初始化心率图表数据
        heart.setTouchEnabled(false);
        heart.setEnabled(false);
        Description ds = new Description();
        ds.setText("");
        heart.setDescription(ds);
        //设置X轴
        XAxis xAxis = heart.getXAxis();
        xAxis.setAxisMaximum(200f);     //设置最大最小值
        xAxis.setAxisMinimum(0);
        xAxis.setDrawGridLines(false);
        xAxis.setEnabled(false);

        //设置Y轴
        YAxis ylAxis = heart.getAxisLeft();
        YAxis yrAxis = heart.getAxisRight();

        ylAxis.setAxisMaximum(40);
        ylAxis.setAxisMinimum(0);
        ylAxis.setDrawGridLines(false);
        ylAxis.setEnabled(false);

        yrAxis.setAxisMaximum(40);
        yrAxis.setAxisMinimum(0);
        yrAxis.setDrawGridLines(false);
        yrAxis.setEnabled(false);

        lds = new LineDataSet(hentries, "心电图表数据");
        lds.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        lds.setDrawCircles(false);      //不绘制小圆点
        ld = new LineData(lds);
        ld.setDrawValues(false);        //不绘制文本信息
        heart.setData(ld);
        heart.invalidate();
    }
    private void initBreathe() {
        //初始化呼吸图表数据
        breathe.setTouchEnabled(false);
        breathe.setEnabled(false);
        Description ds = new Description();
        ds.setText("");
        breathe.setDescription(ds);
        //设置X轴
        XAxis xAxis = breathe.getXAxis();
        xAxis.setAxisMaximum(50f);     //设置最大最小值
        xAxis.setAxisMinimum(0);
        xAxis.setDrawGridLines(false);
        xAxis.setEnabled(false);

        //设置Y轴
        YAxis ylAxis = breathe.getAxisLeft();
        YAxis yrAxis = breathe.getAxisRight();

        ylAxis.setAxisMaximum(20);
        ylAxis.setAxisMinimum(0);
        ylAxis.setDrawGridLines(false);
        ylAxis.setEnabled(false);

        yrAxis.setAxisMaximum(20);
        yrAxis.setAxisMinimum(0);
        yrAxis.setDrawGridLines(false);
        yrAxis.setEnabled(false);

        lds = new LineDataSet(bentries, "呼吸图数据");
//        lds.setMode(LineDataSet.Mode.LINEAR);
//        lds.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        lds.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);
        lds.setDrawCircles(false);      //不绘制小圆点
        ld = new LineData(lds);
        ld.setDrawValues(false);        //不绘制文本信息
        breathe.setData(ld);
        breathe.invalidate();
    }
}
